package ca.ubc.eece310.project;

/**
 * EECE 310 Term Project Fall 2009
 * @author Matthew Fong, Herman Lee, Scott Pearson
 */

import java.util.Collections;

public class ConnectionGetter extends Thread {
	/**
	 * OVERVIEW: A thread to continually update the running processes with ports
	 * 			 open
	 */
	private static CommandExecutor ce = new CommandExecutor();

	// CONSTRUCTOR
	public ConnectionGetter() {
	}

	public void run() {
		/**
		 * MODIFIES: RunningProcess.RunningProcesses 
		 * EFFECTS: Make sure nobody is modifying or accessing RunningProcess.
		 * 			RunningProcesses. Then get the processes with open connections 
		 * 			and store it
		 */
		while (true) { 
			// acquire the lock to modify RunningProcesses
			BandwidthLog.lock.lock();
			RunningProcess.RunningProcesses.clear();
			// get the running processes
			RunningProcess.RunningProcesses = ce.getConnections();
			// sort it
			Collections.sort(RunningProcess.RunningProcesses); 
			// release the lock
			BandwidthLog.lock.unlock();
			try {
				sleep(5000);
			} catch (InterruptedException e) {}
		}
	}
}
